<template>
  <div class="message-item" :class="role">
    <div class="avatar">
      <Icon :icon="role === 'user' ? 'mdi:account' : 'mdi:robot'" />
    </div>
    <div class="content">
      <div class="message" v-html="formattedContent"></div>
      <div class="actions">
        <button class="action-btn" @click="handleCopy" title="复制">
          <Icon icon="mdi:content-copy" />
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Icon } from '@iconify/vue'
import { message } from '@/services/message'

const props = defineProps<{
  role: 'user' | 'assistant'
  content: string
}>()

defineEmits<{
  (e: 'edit'): void
  (e: 'delete'): void
}>()

// 格式化内容
const formattedContent = computed(() => {
  // 确保内容非空
  if (!props.content) return ''
  // 添加pre标签以保持格式
  return `<pre>${props.content}</pre>`
})

const handleCopy = async () => {
  try {
    await navigator.clipboard.writeText(props.content)
    message.success('复制成功')
  } catch (err) {
    message.error('复制失败')
  }
}
</script>

<style lang="scss" scoped>
.message-item {
  display: flex;
  gap: 16px;
  padding: 12px 0;
  margin-bottom: 16px;
  
  &:hover .content .actions {
    opacity: 1;
  }
  
  &.assistant .content {
    background: var(--message-bg);
    border-radius: 4px 16px 16px 16px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    
    :deep(pre) {
      background: transparent;
      padding: 0;
      margin: 0;
      font-family: 'Fira Code', monospace;
    }
  }
  
  &.user {
    flex-direction: row-reverse;
    
    .content {
      background: var(--primary-light);
      border-radius: 16px 4px 16px 16px;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
      
      :deep(pre) {
        background: transparent;
        padding: 0;
        margin: 0;
        font-family: 'Fira Code', monospace;
      }
    }
    
    .actions {
      justify-content: flex-end;
    }
  }

  .avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--avatar-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    
    .iconify {
      font-size: 24px;
      color: white;
    }
  }

  .content {
    flex: 0 1 auto;
    min-width: 100px;
    max-width: 80%;
    padding: 16px;
    border-radius: 12px;
    transition: all 0.2s ease;

    .message {
      line-height: 1.6;
      word-break: break-word;
      font-size: 15px;

      :deep(pre) {
        margin: 0;
        border-radius: 0;
        color: var(--text-color);
        overflow-x: auto;
        white-space: pre-wrap;
        background: transparent !important;
        padding: 0 !important;
        font-family: 'Fira Code', monospace;
      }
      
      :deep(code) {
        font-family: 'Fira Code', monospace;
        background: rgba(0, 0, 0, 0.05);
        padding: 2px 4px;
        border-radius: 4px;
        font-size: 0.9em;
      }
      
      :deep(blockquote) {
        margin: 0.8em 0;
        padding: 0.5em 1em;
        border-left: 4px solid var(--primary-color, #42b883);
        color: var(--text-secondary, #666);
        background: rgba(66, 184, 131, 0.05);
        border-radius: 0 4px 4px 0;
      }
    }

    .actions {
      display: flex;
      gap: 8px;
      margin-top: 10px;
      opacity: 0;
      transition: opacity 0.3s ease;

      .action-btn {
        padding: 6px;
        border: none;
        background: rgba(0, 0, 0, 0.03);
        color: var(--text-secondary);
        border-radius: 6px;
        display: flex;
        align-items: center;
        gap: 4px;
        cursor: pointer;
        font-size: 12px;
        transition: all 0.2s ease;

        &:hover {
          background: rgba(66, 184, 131, 0.1);
          color: var(--primary-color);
        }
      }
    }

    .meta {
      margin-top: 4px;
      font-size: 12px;
      color: var(--text-secondary);
    }
  }
}
</style>